<script setup lang="ts">
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import { useGuessList } from '@/composables'
import NoticeList from '@/components/NoticeList.vue'
import { getCategoryAPI } from '@/services/category'
import type { CategoryItems } from '@/types/category'

// 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
// const getHomeBannerData = async () => {
//   const res = await getHomeBannerAPI()
//   bannerList.value = res.result
// }

// 获取前台分类数据
const categoryList = ref<CategoryItem[]>([])
// const getHomeCategoryData = async () => {
//   const res = await getHomeCategoryAPI()
//   categoryList.value = res.result
// }

// 获取热门推荐数据
const hotList = ref<HotItem[]>([])
// const getHomeHotData = async () => {
//   const res = await getHomeHotAPI()
//   hotList.value = res.result
// }

// ----商品分类 动态渲染 我自己写的--
const newCategoryList = ref<CategoryItems[]>([])
const categoryListData = async () => {
  const res = await getCategoryAPI()
  newCategoryList.value = res.data.info_list
  console.log(res)
}
// 是否加载中标记
const isLoading = ref(false)

// 页面加载
// onLoad(async () => {
//   isLoading.value = true
//   await getHomeCategoryData()
//   isLoading.value = false
// })
onLoad(() => {
  categoryListData()
})
// 猜你喜欢组合式函数调用
const { guessRef, onScrolltolower } = useGuessList()

// 当前下拉刷新状态
const isTriggered = ref(false)

// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开始动画
  isTriggered.value = true
}

// 跳转到通知列表页面
const goToNoticeList = () => {
  uni.navigateTo({
    url: '/pages/notice/list',
  })
}
</script>

<template>
  <view class="index-container">
    <!-- 自定义导航栏 -->
    <CustomNavbar />
    <!-- 滚动容器 -->
    <scroll-view
      enable-back-to-top
      refresher-enabled
      @refresherrefresh="onRefresherrefresh"
      :refresher-triggered="isTriggered"
      @scrolltolower="onScrolltolower"
      class="scroll-view"
      scroll-y
    >
      <!-- <PageSkeleton v-if="isLoading" /> -->
      <template>
        <!-- 自定义轮播图 -->
        <!-- <XtxSwiper :list="bannerList" /> -->
        <!-- 分类面板 -->
        <CategoryPanel :list="newCategoryList" />
        <!-- 热门推荐 -->
        <!-- <HotPanel :list="hotList" /> -->

        <!-- 通知列表组件 -->
        <view class="notice-section">
          <view class="section-header">
            <view class="section-title">通知消息 <i class="iconfont icon-&#xe7ab;"></i> </view>
            <!-- <view class="section-more" @tap="goToNoticeList">更多</view> -->
          </view>
          <NoticeList />
        </view>

        <!-- 猜你喜欢 - 已移至底部 -->
        <!-- <XtxGuess ref="guessRef" /> -->
      </template>
    </scroll-view>
  </view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  overflow: hidden;
}

.index-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.scroll-view {
  flex: 1;
  overflow: hidden;
}

.notice-section {
  margin: 20rpx 30rpx;
  background-color: #fff;
  border-radius: 24rpx;
  padding: 30rpx 0;
  box-shadow: 0 5rpx 20rpx rgba(0, 0, 0, 0.05);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 30rpx;
  margin-bottom: 20rpx;
}

.section-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #333;
}

.section-more {
  font-size: 28rpx;
  color: #999;
}
</style>
